package com.zbkj.admin.controller.publicly;

import com.alibaba.fastjson.JSON;
import com.zbkj.common.annotation.CustomResponseAnnotation;
import com.zbkj.common.response.wx.WxPayBackResult;
import com.zbkj.common.response.wx.WxPayResCode;
import com.zbkj.common.result.CommonResult;
import com.zbkj.common.utils.DateUtil;
import com.zbkj.common.utils.WxPayUtil;
import com.zbkj.service.service.PayCallbackService;
import com.zbkj.service.service.monitor.IZfbMonitorOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Map;
import java.util.Objects;


/**
 * 支付回调
 +----------------------------------------------------------------------
 */
@Slf4j
@RestController
@RequestMapping("api/publicly/payment/callback")
@Api(tags = "支付回调控制器")
@CustomResponseAnnotation
public class PayCallbackController {

    @Autowired
    private PayCallbackService callbackService;

    @Autowired
    private IZfbMonitorOrderService zfbMonitorOrderService;

    @ApiOperation(value = "微信支付回调")
    @RequestMapping(value = "/wechat", method = RequestMethod.POST)
    public String weChat(@RequestBody String request) {
        System.out.println("微信支付回调 request ===> " + request);
        String response = callbackService.wechatPayCallback(request);
        System.out.println("微信支付回调 response ===> " + response);
        return response;
    }

    @ApiOperation(value = "支付宝支付回调 ")
    @RequestMapping(value = "/alipay", method = RequestMethod.POST)
    public String aliPay(HttpServletRequest request) {
        //支付宝支付回调
        System.out.println("支付宝支付回调 request ===> " + JSON.toJSONString(request.getParameterMap()));
        return callbackService.aliPayCallback(request);
    }

    /**
     * 微信退款回调
     */
    @ApiOperation(value = "微信退款回调")
    @RequestMapping(value = "/wechat/refund", method = RequestMethod.POST)
    public String weChatRefund(@RequestBody String request) {
        System.out.println("微信退款回调 request ===> " + request);
        String response = callbackService.weChatRefund(request);
        System.out.println("微信退款回调 response ===> " + response);
        return response;
    }


    /**
     * 微信支付回调
     *
     * @return
     */
    @RequestMapping("/wxCallBack")
    @ApiOperation("微信支付回调")
    public String wxCallBack(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //转成MAP
        Map<String, String> map = WxPayUtil.inputStreamToMap(request.getInputStream());
        String payBackInfo = JSON.toJSONString(map);
        log.info("微信支付回调map：{}", payBackInfo);
        WxPayBackResult backResult = JSON.parseObject(payBackInfo, WxPayBackResult.class);

        if (!Objects.equals(WxPayResCode.PAY_SUCCESS.getResultCode(), backResult.getReturn_code())
                || !Objects.equals(WxPayResCode.PAY_SUCCESS.getResultCode(), backResult.getResult_code())) {
            log.info("微信支付回调失败:{}", payBackInfo);
            return "微信支付回调失败";
        }
        zfbMonitorOrderService.callBack(backResult);
        return WxPayUtil.responseSuccess();

    }


}



